home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
UTIL
/
MEMORY
/
OLD
/
MEM208SRC
/
!Memphis
/
c
/
compress
next >
Wrap
Text File
|
1993-09-09
|
3KB
|
118 lines
/*
* compress.c
* Part of the !Memphis distribution
* (c) bdb/nas, 1991-3
*/
#define SQUASH
#include "kernel.h"
#include "swis.h"
#include "_swis.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compress.h"
/* #define DEBUG */
#ifdef DEBUG
#define DEBUGF printf
#else
#define DEBUGF 1?(void)0:(void)printf
#endif
#ifdef LZW
#ifndef LZW_Compress
#define LZW_Compress 0xCD000
#define LZW_Decompress 0xCD001
#endif
void compress(char *from, int fromlen, char *to, int *tolen/*out*/)
{ int l;
if (_swix(LZW_Compress,_INR(0,4)|_OUT(5),3,from,to,fromlen,fromlen,&l) || l==fromlen)
{ memcpy(to,from,fromlen);
l = fromlen;
}
*tolen = l;
}
void uncompress(char *from, int fromlen, char *to, int tolen)
{ if (tolen == fromlen)
{ memcpy(to,from,fromlen);
return;
}
_swix(LZW_Decompress,_INR(0,4),3,from,to,fromlen,tolen);
}
#endif
#ifdef SQUASH
#ifndef Squash_Compress
#define Squash_Compress 0x42700
#define Squash_Decompress 0x42701
#endif
/* Call Squash module SWI to compress data
* Assumes that tolen >= fromlen
* In the case where compressed >= exceeds uncompressed size (buffer full) then we
* simply copy the data.
* If source data < 32 bytes in length, then it is not work the overheads to compress it.
*/
int compress(char *from, int fromlen, char *to, int tolen)
{ int f,l;
char *work;
if (fromlen >= 32)
{
_swix(Squash_Compress,_INR(0,1)|_OUT(0),8,-1,&f);
work = malloc(f);
_swix(Squash_Compress,_INR(0,5)|_OUT(0)|_OUT(5),0,work,from,fromlen,to,fromlen,&f,&l);
free(work);
if ((f == 2) || (l == 0)) /* out of output space */
{
memcpy(to,from,fromlen);
l = 0;
}
} else
{
memcpy(to,from,fromlen);
l = 0;
}
DEBUGF("compress: %d to %d (buf = %d)\n", fromlen, fromlen-l, tolen);
return(fromlen - l);
}
/* Decompression routine
* This has a couple of messy hacks in it. Since we don't store anywhere whether
* a page is compressed, or copied, we have to use the following rules:
* If the 'compressed size' and the expected file size are equal, then copied
* If 'compressed size' < 32 bytes then copied
* If 'compressed data' starts with byte 0x1f then compressed, else copied
*/
void uncompress(char *from, int fromlen, char *to, int tolen)
{ int f,l;
char *work;
if (((tolen == fromlen) || (fromlen < 32)) && (*from != 0x1f))
{
DEBUGF("copy1: %d\n", fromlen);
memcpy(to,from,fromlen);
return;
}
if (*from == 0x1f)
{
_swix(Squash_Decompress,_INR(0,1)|_OUT(0),8,-1,&f);
work = malloc(f);
_swix(Squash_Decompress,_INR(0,5)|_OUT(0)|_OUT(5),4,work,from,fromlen,to,tolen,&f,&l);
free(work);
DEBUGF("uncompress: %d to %d\n", fromlen, tolen);
} else
{
DEBUGF("copy2: %d\n", fromlen);
memcpy(to,from,fromlen);
return;
}
}
#endif